#include "EditorProgressBarModalWindow.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Widgets/Window.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/ButtonWidget.h"
#include "Widgets/WindowManager.h"
#include "Widgets/TextWidget.h"

void WHE::ShowProgressBarModalWindow(
    const SProgressBarModalWindowCreateParameter & InParameter
    ,TSharedObjectPtr<RWindow> & OutWindow
    )
{
    auto Window=NewObject<RWindow>();
    Window->HideSystemBorder();
    Window->SetWindowSize(SIntPoint(400,70));
    Window->MoveToScreenCenter();
    Window->SetWindowTitle(U"进度条");


    Window->GetRootWidget()->Fill=true;
    Window->GetRootWidget()->FillColor=REditor::Get()
        ->CurrentTheme
        ->BackgroundColor;

    const float BarBackgroundWidth=380;
    auto ProgressBarBackground=NewWidget<RRectangleWidget>(Window->GetRootWidget());
    ProgressBarBackground->X=10;
    ProgressBarBackground->Width=BarBackgroundWidth;
    ProgressBarBackground->Y=10;
    ProgressBarBackground->Height=20;

    ProgressBarBackground->FillColor=REditor::Get()
        ->CurrentTheme
        ->ContextMenuBackgroundColor;

    const float BarPadding=1;
    auto ProgressBar=NewWidget<RRectangleWidget>(ProgressBarBackground);
    ProgressBar->Height=20-BarPadding*2;
    ProgressBar->Anchor.VerticalCenterReference=ProgressBarBackground;
    ProgressBar->X=BarPadding;
    ProgressBar->Width=[
        Percentage=InParameter.Percentage,
        BarMaxWidth=BarBackgroundWidth-BarPadding*2
        ]()->float
    {
        //至少留一个像素的宽度，让用户知道这是个进度条
        return
            CMath::Max(1.0f,BarMaxWidth*Percentage.Get());
    };

    ProgressBar->FillColor=REditor::Get()
        ->CurrentTheme
        ->HighlightColor;

    const float SecondLineY=35;
    auto PercentageText=NewWidget<RTextWidget>(Window->GetRootWidget());
    PercentageText->Text=InParameter.PercentageText;
    PercentageText->Y=SecondLineY;
    PercentageText->X=10;

    if(InParameter.OnCanceled.IsBound())
    {
        auto CancelButton=NewWidget<RButtonWidget>(Window->GetRootWidget());
        CancelButton->Text=U"取消";
        CancelButton->BackgroundStyle.CornerRadius=5;
        CancelButton->Y=SecondLineY;
        CancelButton->Height=25;
        CancelButton->Width=50;
        CancelButton->Anchor.RightReference=ProgressBarBackground;
        CancelButton->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Right;
        CancelButton->OnClicked.BindLambda(
            [OnClicked=InParameter.OnCanceled](const SMouseButtonEvent&) mutable->bool
        {
            OnClicked.Broadcast();
            return true;
        });

        RButtonWidgetWeakPtr WeakButton=CancelButton;
        CancelButton->BackgroundStyle.FillColor=[
            WeakButton
        ]()->SColor
        {
            if(WeakButton->IsHovered())
            {
                return REditor::Get()->CurrentTheme->HighlightColor_Hover;
            }
            return REditor::Get()->CurrentTheme->HighlightColor;
        };
        
    }

    OutWindow=Window;
    Window->Show();
    RWindowManager::Get()->MakeWindowModal(Window);
}

void WHE::CloseProgressBarModalWindow(TSharedObjectPtr<RWindow> InWindow)
{
    InWindow->Close();
}
